﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Globalization;
using System.Web.Configuration;
using System.IO;

public class common
{
    #region[FormatDate_2_Longdate]
    public static string FormatDate_2_Longdate(DateTime date, string lang)
    {
        string datename = date.DayOfWeek.ToString();
        switch (date.DayOfWeek.ToString())
        {
            case "Saturday":
                if (lang.Equals("VI"))
                {
                    datename = "Thứ bảy";
                    //datename = "Saturday";
                }
                break;
            case "Sunday":

                if (lang.Equals("VI"))
                {
                    datename = "Chủ nhật";
                    //datename = "Sunday";
                    break;
                }
                break;
            case "Monday":

                if (lang.Equals("VI"))
                {
                    datename = "Thứ hai";
                    //datename = "Monday";
                    break;
                }
                break;
            case "Tuesday":

                if (lang.Equals("VI"))
                {
                    datename = "Thứ ba";
                    //datename = "Tuesday";
                    break;
                }
                break;
            case "Wednesday":
                if (lang.Equals("VI"))
                {
                    datename = "Thứ tư";
                    //datename = "Wednesday";
                    break;
                }

                break;
            case "Thursday":
                if (lang.Equals("VI"))
                {
                    datename = "Thứ năm";
                    //datename = "Thursday";
                    break;
                }
                break;
            case "Friday":
                if (lang.Equals("VI"))
                {
                    datename = "Thứ sáu";
                    //datename = "Friday";
                    break;
                }
                break;
        }
        return datename.ToString() + ", Tháng " + date.ToString("M yyyy hh:mm");
    }
    #endregion
    #region[RemoveUnicode]
    public static string RemoveUnicode(string s)
    {
        string stFormD = s.Normalize(NormalizationForm.FormD);
        StringBuilder sb = new StringBuilder();

        for (int ich = 0; ich < stFormD.Length; ich++)
        {
            UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
            if (uc != UnicodeCategory.NonSpacingMark)
            {
                sb.Append(stFormD[ich]);
            }
        }
        string temp = (sb.ToString().Normalize(NormalizationForm.FormC));
        temp = temp.Replace("đ", "d");
        return temp.Replace("Đ", "D");
    }
    #endregion
    #region[userofline]
    public static void userofline()
    {
        using (SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand dbCmd = new SqlCommand("update tbvisistor set iuseronline=iuseronline-1 where Id=(select top 1 Id from tbvisistor order by Id)", dbConn))
            {
                dbCmd.CommandType = CommandType.Text;
                dbConn.Open();
                dbCmd.ExecuteNonQuery();
                dbConn.Close();
            }
        }
    }
    #endregion
    #region Get DataTable
    public static DataTable Get_DataTable(SqlCommand cmd)
    {
        using (DataSet ds = new DataSet())
        {
            using (SqlDataAdapter da = new SqlDataAdapter())
            {
                da.SelectCommand = cmd;
                da.SelectCommand.Connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                da.Fill(ds);
                da.SelectCommand.Connection.Close();
                return ds.Tables[0];
            }
        }
    }
    #endregion
    #region Get_DataSet
    public static DataSet Get_DataSet(SqlCommand cmd)
    {
        using (DataSet ds = new DataSet())
        {
            using (SqlDataAdapter da = new SqlDataAdapter())
            {
                da.SelectCommand = cmd;
                da.SelectCommand.Connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                da.Fill(ds);
                da.SelectCommand.Connection.Close();
                return ds;
            }
        }
    }
    #endregion
    #region[imgdownload]
    public static string imgdownload(string path)
    {
        string Chuoi="";
        if (path.IndexOf(".doc") > 0 || path.IndexOf(".Doc") > 0)
        {
            Chuoi = "<img src='images/word.gif' style='border:0px; width:45px; height:44px'/>";
        }
        else if (path.IndexOf(".pdf") > 0 || path.IndexOf(".Pdf") > 0)
        {
            Chuoi = "<img src='images/pdf.gif' style='border:0px; width:42px; height:42px'/>";
        }
        else if (path.IndexOf(".zip") > 0 || path.IndexOf(".Zip") > 0 || path.IndexOf(".rar") > 0)
        {
            Chuoi = "<img src='images/zip.gif' style='border:0px; width:52px; height:43px'/>";
        }
        else if (path.IndexOf(".xls") > 0 || path.IndexOf(".Xls") > 0)
        {
            Chuoi = "<img src='images/excel.gif' style='border:0px; width:43px; height:43px'/>";
        }
        return Chuoi;
    }
    #endregion
    #region[Xep loai danh gia]
    public static string TypeAssessment(string PointStaff, string uID)
    {
        string Chuoi = "";
        double dTotalPoint = 0;
        double dPointStaff = double.Parse(PointStaff.ToString());
        double dPointType = 0;
        double dPointMax = 0;
        List<tbConfigDATA> list = tbConfigDB.tbConfig_GetAll();
        if (list.Count > 0)
        {
            dPointType = double.Parse(list[0].coPoitType);
            dPointMax = double.Parse(list[0].coMaxPoint);
        }
        list.Clear();
        list = null;
        List<tbCriteriaDATA> listTC = tbCriteriaDB.tbCriteria_GetByuID(uID);
        if (listTC.Count > 0)
        {
            dTotalPoint = dPointMax * double.Parse(listTC.Count.ToString());
        }
        listTC.Clear();
        listTC = null;
        
        if (dPointStaff >= dTotalPoint)
        {
            Chuoi = "Xuất sắc";
        }
        else if (dPointStaff == (dTotalPoint - dPointType))
        {
            Chuoi = "Giỏi";
        }
        else if (dPointStaff > dTotalPoint - ((dPointType * 2) - 1))
        {
            Chuoi = "Khá";
        }
        else if (dPointStaff > dTotalPoint - ((dPointType * 3) - 1))
        {
            Chuoi = "Trung bình";
        }
        else if (dPointStaff > dTotalPoint - ((dPointType * 4) - 1))
        {
            Chuoi = "Yếu";
        }
        else
        {
            Chuoi = "Kém";
        }
        return Chuoi;
    }
    #endregion
    #region[So]
    public static string RoundNum(string so)
    {
        string chuoi = "0";
        if (so.ToString().Trim().Length > 0)
        {
            chuoi = Convert.ToString(Math.Round(double.Parse(so), 1));
        }
        return chuoi;
    }
    #endregion
    #region[Checkpath]
    public static string Checkpath(string path)
    {
        string Chuoi = "";
        string Duoi = "";
        if (path.Length > 0)
        {
            Duoi = System.IO.Path.GetExtension(path);
        }
        if (Duoi == ".doc" || Duoi == ".xls" || Duoi == ".pdf" || Duoi == ".rar" || Duoi == ".zip")
        {
            Chuoi = path;
        }
        else
        {
            Chuoi = "";
        }
        return Chuoi;
    }
    #endregion
    #region [Check Exit sheetname]
    public static bool ExitSheetName(string FileName, string iSheetName)
    {
        OleDbConnection con = null;
        DataTable dt = null;
        String conStr = "";
        bool bReturn = false;
        if (Path.GetExtension(FileName) == ".xlsx")
        {
            conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
        }
        else
        {
            conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=Excel 8.0;";
        }
        try
        {
            con = new OleDbConnection(conStr);
            con.Open();
            dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            con.Close();
            con = null;
            if (dt == null)
            {
                bReturn = false;
            }
            foreach (DataRow row in dt.Rows)
            {
                //excelSheetNames = row["TABLE_NAME"].ToString().Substring(;
                string sheetName = (string)row["TABLE_NAME"];
                if (sheetName.EndsWith("$"))
                {
                    sheetName = sheetName.Substring(0, sheetName.Length - 1);
                    if (sheetName.ToLower() == iSheetName.ToLower())
                    {
                        bReturn = true;
                        break;
                    }
                }
            }

        }
        catch { }
        return bReturn;
    }
    #endregion
}
